function [TS, res] = portfolio_risk_calc(h_file, bench_file, HL)
% want to take holding vector and calculate portfolio risk, beta, and active risk

if nargin < 1, h_file = 'my60_40_wDFA.csv'; end
if nargin < 2, bench_file = 'bench60_40.csv'; end
if nargin < 3. HL = 500; end

res = struct;

root_path = getenv('wcm_root');
data_path = [root_path 'data/yahoo_data/'];
h_path = [root_path 'data/holdings/'];

ret = readTS(data_path,'all_ret.csv');
h = readTS(h_path,h_file);
if isstruct(ret)
  missingIdx = ~ismember(h.header, ret.header);
  firstdate = ret.dates(1);
  lastdate = ret.dates(end);
else
  missingIdx = 1:length(h.header);
  firstdate = 20100101;
  lastdate = mdt2TSdt(today());
end
if sum(missingIdx) 
  yahoo_data_loader(h.header(missingIdx), datestr(TSdatenum(firstdate),'mm/dd/yyyy'), datestr(TSdatenum(lastdate),'mm/dd/yyyy'));
end

h_b = readTS(h_path, bench_file);
if isstruct(ret)
  missingIdx = ~ismember(h_b.header, ret.header);
else 
  missingIdx = 1:length(h_b.header);
end

if sum(missingIdx) 
  yahoo_data_loader(h_b.header(missingIdx), datestr(TSdatenum(firstdate),'mm/dd/yyyy'), datestr(TSdatenum(lastdate),'mm/dd/yyyy'));
end
ret = readTS(data_path,'all_ret.csv');

h_all = joinTS(buildTS(h_b),buildTS(h),'headers','union');
h_all.data(:) = 0;
h = insertTS(h, h_all);
h_b = insertTS(h_b, h_all);
ret = joinTS(ret, h.header, 'headers');
%ret = joinTS(ret, [20130524 20140717], 'daterange');
res.dates = ret.dates;
res.ret = ret;

my_covm = single_covmTS(ret, HL, 250);
res.covm = my_covm;

%sector_map = readTS(sector map);
%
%sectors = h;
%sectors.data = sector_map.data(strcmp(sector_map.header, sectors.header));


h_eq = h;
h_eq.data(:) = 0;
h_eq.data(1,strcmp(h.header, 'SPY')) = 1;

%h = addTS(multTS(h_eq,0.6),multTS(h,0.4));

beta_alloc = buildTS([],h.header, {'beta'}, 'rownames');
beta_alloc.data = h.data .* (h_b.data * my_covm.data) / (h_b.data * my_covm.data * h_b.data');
res.beta = sum(beta_alloc.data);

risk_alloc = buildTS([],h.header, {'risk'}, 'rownames');
risk_alloc.data = h.data .* (h.data * my_covm.data);
res.risk = sqrt(sum(risk_alloc.data));

h_active = subtractTS(h, multTS(h_b,res.beta));
act_risk_alloc = buildTS([],h.header, {'active_risk'}, 'rownames');
act_risk_alloc.data = h_active.data .* (h_active.data * my_covm.data);
res.act_risk = sqrt(sum(act_risk_alloc.data));
%active_beta_alloc = buildTS([],h.header, {'active_beta'}, 'rownames');
%active_beta_alloc.data = h_active.data .* (h_b.data * my_covm.data) / (h_b.data*my_covm.data*h_b.data');
%active_beta  = sum(active_beta_alloc.data);

equity_beta_alloc = buildTS([],h.header, {'eq_beta'}, 'rownames');
equity_beta_alloc.data = h.data .* (h_eq.data * my_covm.data) / (h_eq.data*my_covm.data*h_eq.data');
res.equity_beta = sum(equity_beta_alloc.data);

bench_risk_alloc = buildTS([],h.header, {'bench_risk'}, 'rownames');
bench_risk_alloc.data = h_b.data .* (h_b.data * my_covm.data);
res.bench_risk = sqrt(h_b.data*my_covm.data*h_b.data');
res.pret =  ret.data*h.data';
res.bench_ret = ret.data*h_b.data';
res.pcret = cumsum(res.pret);
res.bench_cret = cumsum(res.bench_ret);

TS = buildTS(h.data, h.header, {'holdings'}, 'rownames');
TS = joinTS(TS, buildTS(sqrt(diag(my_covm.data)'), h.header, {'asset_risk'}, 'rownames'), 'rownames', 'union');
TS = joinTS(TS, risk_alloc, 'rownames', 'union');
TS = joinTS(TS, act_risk_alloc, 'rownames', 'union');
TS = joinTS(TS, beta_alloc, 'rownames', 'union');
TS = joinTS(TS, equity_beta_alloc, 'rownames', 'union');
TS = joinTS(TS, bench_risk_alloc, 'rownames', 'union');

writeTS(TS, h_path, [h_file(1:end-4) '_risk.csv']);
end


